home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: presby.edu!jtbell
- From: jtbell@presby.edu (Jon Bell)
- Subject: Re: C++ beginner quesion on data member access.
- Message-ID: <Dpnpqz.2Io@presby.edu>
- Date: Wed, 10 Apr 1996 17:25:46 GMT
- References: <4kgb76$r3s@HOPPER.ACM.ORG>
- Organization: Presbyterian College, Clinton, South Carolina USA
-
- Ken Varn <varnk@e62.diebold.com> wrote:
- > but what do
- >I gain if all I am doing is providing a member function to get the private
- >data or set the private data. i.e. why call a getData() function that
- >basically just returns the private data member as opposed to just declaring
- >the pviate data member as public.
-
- At some point in the future, you might want to change the way the member
- data is organized, after which the value in question might not be stored
- directly as a member data item, but can be calculated from other member
- data. The classic example of this is rectangular versus polar
- representation for complex numbers:
-
- class Complex
- {
- public
- double RealPart (void) const { return RealPart_; };
- double ImagPart (void) const { return ImagPart_; };
- //etc.
- private
- double RealPart_, ImagPart_;
- };
-
- versus
-
- class Complex
- {
- public
- double RealPart (void) const;
- double ImagPart (void) const;
- //etc.
- private
- double Magnitude_, PolarAngle_;
- };
-
- In the second version, the real part and imaginary part can be calculated
- from the magnitude and polar angle.
-
- --
- Jon Bell <jtbell@presby.edu> Presbyterian College
- Dept. of Physics and Computer Science Clinton, South Carolina USA
-